/*
 * TODO: Enter copyright statement here.
 */
package com.googlecode.wcbunittests.helloworld.element.validator;

import java.util.Calendar;

import com.googlecode.wcbunittests.helloworld.element.HelloWorldElementFBO;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * This validator validates if the date specified by the element is in the past.
 */
public class PastDateValidator implements Validator {
    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public boolean supports(Class clazz) {
        return clazz.isAssignableFrom(HelloWorldElementFBO.class);
    }

    /**
     * {@inheritDoc}
     */
    public void validate(Object target, Errors errors) {
        Calendar date = ((HelloWorldElementFBO) target).getDate();
        if (date != null && !date.getTime().before(Calendar.getInstance().getTime())) {
            errors.rejectValue("date", "element.validator.datenotinpast");
        }
    }
}
